package com.app.controller;

/*
  @author 阿伟
  @create 2020-11-23:14:53
  聊一聊Controller
*/

import com.app.condition.ConsultCondition;
import com.app.dao.ConsultDao;
import com.app.service.ConsultService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.common.entity.Code;
import com.common.entity.Consult;
import com.common.entity.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Api(tags = "聊一聊模块")
@RestController
@RequestMapping("/consult")
public class ConsultController {

    @Autowired
    ConsultService consultService;

    @Autowired
    ConsultDao consultDao;

    @ApiOperation(value = "通过ID查询聊一聊数据")
    @GetMapping("/one")
    public ResponseResult getConsultById(
            @ApiParam(name = "consultId",value = "聊一聊ID",required = true)
            @RequestParam("consultId") String consultId){
        Consult consult = consultDao.selectById(Integer.parseInt(consultId));
        return ResponseResult.success(Code.REQUEST_SUCCESS).Data("consult",consult);
    }

    @ApiOperation(value = "分页查询聊一聊，带条件")
    @GetMapping("/page")
    public ResponseResult getConsultByLimit(
            @ApiParam(name = "pageIndex",value = "当前页",required = true)
            @RequestParam("pageIndex") String pageIndex,
            @ApiParam(name = "pageSize",value = "页大小",required = true)
            @RequestParam("pageSize") String pageSize,
            @ApiParam(name = "consultCondition",value = "查询的条件",required = false)
                    ConsultCondition consultCondition){
        Long index = Long.parseLong(pageIndex);
        Long size = Long.parseLong(pageSize);
        //page对象
        Page<Consult> page = new Page<>(index,size);
        //调用查询的方法
        consultService.selectConsultByLimit(page,consultCondition);
        List<Consult> Consults = page.getRecords();
        return ResponseResult.success(Code.REQUEST_SUCCESS).Data("user",Consults);
    }

    @ApiOperation(value = "添加聊一聊")
    @GetMapping("/insert")
    public ResponseResult insertCompliant(
            @RequestParam("multipartFile") MultipartFile multipartFile,
            @ApiParam(name = "text",value = "内容",required = true)
            @RequestParam("text") String text,
            @ApiParam(name = "phone",value = "手机号",required = true)
            @RequestParam("phone") String phone){

        return consultService.addConsult(multipartFile, text, phone);
    }

    @ApiOperation(value = "查询所有的聊一聊数据")
    @GetMapping("/getallConsult")
    public ResponseResult getAllConsult(){
        return consultService.getAllConsult();
    }
}
